package com.itheima.d2_ThreadPool;

import java.util.concurrent.*;

/**
 *
 */
public class Callable_ThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(3,
                5,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5)
                ,Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        //线程池任务的处理使用Future,提交为submit
    Future<String> f1= executorService.submit(new MyCallable(100));
    Future<String> f2= executorService.submit(new MyCallable(50));
    Future<String> f3= executorService.submit(new MyCallable(40));
    Future<String> f4= executorService.submit(new MyCallable(30));

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());


    }
}


class MyCallable implements Callable<String> {
    private int n;
    public void setN(int n) {
        this.n = n;
    }
    public MyCallable(int n) {
        this.n = n;
    }
    @Override
    public String call() throws Exception {
        int sum =0;
        for (int i = 0; i <=n; i++) {
            sum+=i;
        }
        return "子进程返回的结果是" + sum;
    }
}